<?php
	class students extends spModel{
		var $pk = "id"; // 每个学年唯一的标志，可以称为主键
		var $table = "student"; // 数据表的名称
	
	    var $validator = array(
			"rules" => array( // 规则
				'account' => array(  // 这里是对username的验证规则
					'required' => true, // username不能为空
					'minlength' => 1,  // username长度不能小于5
					'maxlength' => 50, // username长度不能大于15
				),	
				'password' => array(
					'required' => true, // username不能为空
					'minlength' => 6,  // username长度不能小于5
				),	
				
			),
			"messages" => array( // 提示信息
				'account' => array(
					'required' => '账号不能为空',
					'minlength' => '账号不能少于1个字符',
					'maxlength' => '账号不能大于50字符',
				),
				'password' => array(
					'required' => '密码不能为空',
					'minlength' => '密码不能少于6个字符',
				),
			)
		);
	
	
		public function user_login($uname, $password){
			if($result = $this->spValidator(array("account"=>$uname, "password"=>$password))) {
                foreach($result as $item)
                    throw new Exception($item[0]);
            }
            $condition = "account = '".$uname."'";
            if(!$this->find($condition))
                throw new Exception("账号输入有误，请重新输入！");
			$condition .= 'and password = "'. md5($password).'"';
			if(!$student_rs = $this->find($condition))
				throw new Exception("密码输入有误，请重新输入！");    
			if($student_rs["status"] == '1'){
				$this->save_login_cookie(1, $uname, $password);
				if(substr(strtolower($student_rs["imgheader"]), 0, 4) != "http")
					$student_rs["imgheader"] = WEB_ROOT . "/" . $student_rs["imgheader"];
				$_SESSION["sswcdc_user"] = $student_rs;
				return $student_rs;
			}elseif($student_rs["status"] == '0'){
				throw new Exception("该账号未激活，无法登录");
			}else{
				throw new Exception("该账号已屏蔽，无法登录");
			}
		}
		
		public function save_login_cookie($type, $username, $password){
			import("Common.php");
			$cookie_array = array("t"=>$type, "u"=>$username, "p"=>Common::authcode($password));
			setcookie("sswcdc_user", serialize($cookie_array), time() + 3600 * 24 * 365, COOKIE_PATH);
		}

		public function checkPass($uid,$account,$pass){
		    $where = array('id' => $uid, 'account'=>$account,'password'=>md5($pass) );
		    if( $student_rs = $this->find($where)){
                return $student_rs;
            }else{
		        throw new Exception('密码输入有误，请您确认后重新输入');
            }

        }
		
		
	}
?>